package com.hnevc.myspeaker.Shopping;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.hnevc.myspeaker.R;
import com.hnevc.myspeaker.entity.Category;

import java.util.ArrayList;

public class CategoryActivity extends AppCompatActivity {
    ArrayList<Category> categoryList;
    GridView mGvCategoryView;
    MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);
        initData();
        initView();
        initAdapter();

    }

    private void initAdapter() {
        adapter = new MyAdapter();
        mGvCategoryView.setAdapter(adapter);

    }

    private void initData() {
        categoryList = new ArrayList<>();
        Category c = new Category("mobilephone.png","手机");
        categoryList.add(c);
        c = new Category("baowenbei.png","保温杯");
        categoryList.add(c);
        c = new Category("diancilu.png","电磁炉");
        categoryList.add(c);
        c = new Category("diancuifeng.png","电吹风");
        categoryList.add(c);
        c = new Category("erji.png","耳机");
        categoryList.add(c);
        c = new Category("kouhong.png","口红");
        categoryList.add(c);
        c = new Category("luyouqi.png","路由器");
        categoryList.add(c);
        c = new Category("xifashui.png","洗发水");
        categoryList.add(c);
        c = new Category("mobilephone.png","手机");
        categoryList.add(c);
    }

    private void initView() {
        mGvCategoryView = findViewById(R.id.gv_category);

    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return categoryList.size();
        }

        @Override
        public Object getItem(int position) {
            return categoryList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View inflate = View.inflate(CategoryActivity.this, R.layout.category_gridview_item
                    , null);
            ImageView ivCategoryPic = inflate.findViewById(R.id.iv_category_pic);

            TextView tvCategoryName = inflate.findViewById(R.id.tv_category_name);


            Category categroy = categoryList.get(position);
            ivCategoryPic.setImageResource(getImageIdentify(categroy));
            tvCategoryName.setText(categroy.getCategoryName());

            inflate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(CategoryActivity.this,ProductListActivity.class));
                }
            });

            return inflate;
        }
    }
    private int getImageIdentify(Category categroy) {
        int identifier = getResources()
                .getIdentifier(categroy.getCategoryPic().split("\\.")[0]
                        , "mipmap", getPackageName());
        return identifier;

    }

}
